package com.baoxin.chenxixc4coach;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.baoxin.chenxixc4coach.adapter.OrderDetailAdapter;
import com.baoxin.chenxixc4coach.bean.OrderDetail;
import com.baoxin.chenxixc4coach.utils.TimeUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class OrderDetailActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private OrderDetailAdapter adapter;
    private List<OrderDetail> orderDetailList;
    private TextView mStartDay;
    private TextView mEndDay;
    private String startYMD;
    private String endYMD;
    private TextView mStartTime;
    private TextView mEndTime;
    private Calendar currentCalendar;
    private Calendar startCalendar;
    private Calendar endCalendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mStartDay = findViewById(R.id.start_day);
        mEndDay = findViewById(R.id.end_day);
        mStartTime = findViewById(R.id.start_time);
        mEndTime = findViewById(R.id.end_time);

        currentCalendar = Calendar.getInstance();
        startCalendar = Calendar.getInstance();
        endCalendar = Calendar.getInstance();

        int year = currentCalendar.get(Calendar.YEAR);
        int month = currentCalendar.get(Calendar.MONTH);
        int dayOfMonth = currentCalendar.get(Calendar.DAY_OF_MONTH);
        month += 1;
        startYMD = year + "/" + month + "/" + dayOfMonth;
        endYMD = startYMD;
        mStartDay.setText(startYMD);
        mEndDay.setText(endYMD);

        mStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                TimePickerDialog timePickerDialog = new TimePickerDialog(OrderDetailActivity.this,
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                StringBuilder sb = new StringBuilder();
                                if (hourOfDay > 9) {
                                    sb.append(hourOfDay);
                                } else {
                                    sb.append("0").append(hourOfDay);
                                }
                                sb.append(":");
                                if (minute > 9) {
                                    sb.append(minute);
                                } else {
                                    sb.append("0").append(minute);
                                }
                                ((TextView) v).setText(sb);
                            }
                        }, 8, 0, true);
                timePickerDialog.show();
            }
        });

        mEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                TimePickerDialog timePickerDialog = new TimePickerDialog(OrderDetailActivity.this,
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                StringBuilder sb = new StringBuilder();
                                if (hourOfDay > 9) {
                                    sb.append(hourOfDay);
                                } else {
                                    sb.append("0").append(hourOfDay);
                                }
                                sb.append(":");
                                if (minute > 9) {
                                    sb.append(minute);
                                } else {
                                    sb.append("0").append(minute);
                                }
                                ((TextView) v).setText(sb);
                            }
                        }, 8, 0, true);
                timePickerDialog.show();
            }
        });

        recyclerView = findViewById(R.id.recyclerview);
        orderDetailList = new ArrayList<>();
        adapter = new OrderDetailAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter.setData(orderDetailList);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(adapter);
        initData();


    }

    /**
     * 起始日
     *
     * @param view
     */
    public void onStartDay(View view) {
        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        month += 1;
                        startYMD = year + "/" + month + "/" + dayOfMonth;
                        startCalendar.setTime(TimeUtil.StrToDate(startYMD, "yyyy/MM/dd"));
                        mStartDay.setText(startYMD);
                        //更新end
                        resetEndDate();
                    }
                },
                currentCalendar.get(Calendar.YEAR),
                currentCalendar.get(Calendar.MONTH),
                currentCalendar.get(Calendar.DAY_OF_MONTH));
        //    datePickerDialog.getDatePicker().setMinDate(currentCalendar.getTimeInMillis());
        datePickerDialog.show();
    }

    /**
     * 终止日
     *
     * @param view
     */
    public void onEndDay(View view) {
        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        month += 1;
                        endYMD = year + "/" + month + "/" + dayOfMonth;
                        endCalendar.setTime(TimeUtil.StrToDate(endYMD, "yyyy/MM/dd"));
                        mEndDay.setText(endYMD);
                    }
                },
                startCalendar.get(Calendar.YEAR),
                startCalendar.get(Calendar.MONTH),
                startCalendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.getDatePicker().setMinDate(startCalendar.getTimeInMillis());
        datePickerDialog.show();
    }

    /**
     * 设置初始日期后结束，结束日期相应进行合理变化
     */
    private void resetEndDate() {
        if (startCalendar.getTimeInMillis() > endCalendar.getTimeInMillis()) {
            endCalendar.setTime(startCalendar.getTime());
            mEndDay.setText(startYMD);
            endYMD = startYMD;
        }
    }

    /**
     * 搜索时间段
     *
     * @param view
     */
    public void onSearch(View view) {
        Date startDate = TimeUtil.StrToDate(startYMD + " " + mStartTime.getText().toString(), "yyyy/MM/dd HH:mm");
        Date endDate = TimeUtil.StrToDate(endYMD + " " + mEndTime.getText().toString(), "yyyy/MM/dd HH:mm");
    }

    private void initData() {
        OrderDetail detail = new OrderDetail();
        detail.setIcon("");
        detail.setLianxi(true);
        detail.setPingjia(true);
        detail.setName("张三");
        detail.setTime("2018-9-9 9:00-10:00");

        OrderDetail detail2 = new OrderDetail();
        detail2.setIcon("");
        detail2.setLianxi(true);
        detail2.setPingjia(true);
        detail2.setName("张三");
        detail2.setTime("2018-9-9 9:00-10:00");

        OrderDetail detail3 = new OrderDetail();
        detail3.setIcon("");
        detail3.setLianxi(true);
        detail3.setPingjia(true);
        detail3.setName("张三");
        detail3.setTime("2018-9-9 9:00-10:00");


        OrderDetail detail4 = new OrderDetail();
        detail4.setIcon("");
        detail4.setLianxi(true);
        detail4.setPingjia(true);
        detail4.setName("张三");
        detail4.setTime("2018-9-9 9:00-10:00");


        OrderDetail detail5 = new OrderDetail();
        detail5.setIcon("");
        detail5.setLianxi(true);
        detail5.setPingjia(true);
        detail5.setName("张三");
        detail5.setTime("2018-9-9 9:00-10:00");


        OrderDetail detail6 = new OrderDetail();
        detail6.setIcon("");
        detail6.setLianxi(true);
        detail6.setPingjia(true);
        detail6.setName("张三");
        detail6.setTime("2018-9-9 9:00-10:00");


        OrderDetail detail7 = new OrderDetail();
        detail7.setIcon("");
        detail7.setLianxi(true);
        detail7.setPingjia(true);
        detail7.setName("张三");
        detail7.setTime("2018-9-9 9:00-10:00");


        orderDetailList.add(detail);
        orderDetailList.add(detail2);
        orderDetailList.add(detail3);
        orderDetailList.add(detail4);
        orderDetailList.add(detail5);
        orderDetailList.add(detail6);
        orderDetailList.add(detail7);
    }


}
